package cn.momoky.ccool.service;

import cn.momoky.ccool.cache.UploadTaskCache;
import cn.momoky.ccool.pojo.UploadTask;
import cn.momoky.ccool.utils.UUID;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@AllArgsConstructor
public class UploadTaskService {

    private final UUID uuid;
    private final UploadTaskCache uploadTaskCache;

    public UploadTask getUploadTask(Long nodeId) {
        return uploadTaskCache.getUploadTask(nodeId);
    }

    public void add(UploadTask uploadTask) {
        uploadTask.setId(uuid.get());
        uploadTaskCache.addUploadTask(uploadTask);
    }

    public double addChunkSeq(Long id, Integer seq) {
        int uploadedChunkCount = uploadTaskCache.addChunkSeq(id, seq);
        int chunkCount = uploadTaskCache.getChunkCount(id);
        return (uploadedChunkCount * 1.0) / chunkCount;
    }

    public void del(Long id) {
        uploadTaskCache.del(id);
    }


}
